package com.flight.trans.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.activity.EnterPortHandoverActivity;
import com.flight.trans.activity.EnterPortWarehousingDetailsActivity;
import com.flight.trans.adapter.EnterPortWarehousingAdapter;
import com.flight.trans.bean.EnterPortWarehousingBean.EnterPortWarehousingData;
import com.flight.trans.bean.FlightBean.FlightData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.util.ChinaToPinyin;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ProgressUtils;
import com.flight.trans.view.HeaderTitleView;
import com.flight.trans.base.BaseFragment;
import com.flight.trans.view.dialog.FlightDialog;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 进港入库
 */
public class EnterPortWarehousingFragment extends BaseFragment {

    private HeaderTitleView mHeaderView ;

    private TextView mTvFlightNumber ,//航班号
            mTvFlightDate ,//航班日期
            mTvAta ,//进港实落
            mTvRouteLine ,//航线
            mTvCount ,//件数
            mTvWeight ,//重量
            mTvInventoryPerson ,//分拣库管
            mTvOverTime ;//分拣完毕时间

    private TwinklingRefreshLayout mRefreshLayout ;
    private RecyclerView mRvContainer ;
    private RelativeLayout mRlContentNull ;

    private EnterPortWarehousingAdapter mWarehousingAdapter ;
    private List<EnterPortWarehousingData> mWarehousingDataList ,mSearchDataList;

    private int mSortState = -1;

    @Override
    public int setContentView() {
        return R.layout.fragment_enter_port_warehousing;
    }

    @Override
    public void initView() {
        mHeaderView = mView.findViewById(R.id.mHeaderView);

        mTvFlightNumber = mView.findViewById(R.id.mTvFlightNumber);
        mTvFlightDate = mView.findViewById(R.id.mTvFlightDate);
        mTvAta = mView.findViewById(R.id.mTvAta);
        mTvRouteLine = mView.findViewById(R.id.mTvRouteLine);
        mTvCount = mView.findViewById(R.id.mTvCount);
        mTvWeight = mView.findViewById(R.id.mTvWeight);
        mTvInventoryPerson = mView.findViewById(R.id.mTvInventoryPerson);
        mTvOverTime = mView.findViewById(R.id.mTvOverTime);

        mRefreshLayout = mView.findViewById(R.id.mRefreshLayout);
        mRvContainer = mView.findViewById(R.id.mRvContainer);
        mRlContentNull = mView.findViewById(R.id.mRlContentNull);
    }

    @Override
    public void initData() {
        initHeaderView();

        mTvFlightNumber.setOnClickListener(this::onClickSort);
        mTvFlightDate.setOnClickListener(this::onClickSort);
        mTvAta.setOnClickListener(this::onClickSort);
        mTvRouteLine.setOnClickListener(this::onClickSort);
        mTvCount.setOnClickListener(this::onClickSort);
        mTvWeight.setOnClickListener(this::onClickSort);
        mTvInventoryPerson.setOnClickListener(this::onClickSort);
         mTvOverTime.setOnClickListener(this::onClickSort);

        setRefreshListener(mRefreshLayout);
    }

    @Override
    public void initConnect() {
        if(mHeaderView == null) return;

        valueParams = new HttpParams();
        valueParams.put(Constant.Key_Is_Done , mHeaderView.isDone());
        if(!TextUtils.isEmpty(mHeaderView.getSearchContent()))
            valueParams.put(Constant.Key_Flight_No , mHeaderView.getSearchContent());
        if(mHeaderView.isDone())
            valueParams.put(Constant.Key_Flight_Date , mHeaderView.getSelectDate());

        NetworkUtils.getEnterPortWarehousingList(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mWarehousingDataList = (List<EnterPortWarehousingData>) body;

                mSearchDataList = mWarehousingDataList ;

                initAdapter();
            }
        }, valueParams);
    }

    private void initAdapter(){
        mRlContentNull.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.GONE : View.VISIBLE);

        if(mSearchDataList != null)
            Collections.sort(mSearchDataList, new SortComparator(mSortState));

        if(mWarehousingAdapter == null) {
            mWarehousingAdapter = new EnterPortWarehousingAdapter(mActivity, mSearchDataList);
            mRvContainer.setAdapter(mWarehousingAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mWarehousingAdapter.setOnItemClickListener(new WarehousingItemClickListener());
        }else {
            mWarehousingAdapter.updateAdapter(mSearchDataList);
        }
    }

    /**
     * 条目点击事件
     */
    private class WarehousingItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {

            NetworkUtils.checkPageLock(new OkGoBackListener(mActivity) {
                @Override
                public void onSuccess(Object body) {
                    mIntent = new Intent(mActivity , EnterPortWarehousingDetailsActivity.class);
                    mBundle = new Bundle();
                    mBundle.putInt(Constant.FlightID , mSearchDataList.get(position).getFlightId());
                    mBundle.putBoolean(Constant.IsEditor , !mHeaderView.isDone());
                    mIntent.putExtras(mBundle);
                    turnActForResult(mIntent);
                }
            }, ConstantInfo.IN_STORAGE_IN , mSearchDataList.get(position).getFlightId());
        }

        @Override
        public void onLongClick(View view, int position) {
            mIntent = new Intent(mActivity , EnterPortHandoverActivity.class);
            mBundle = new Bundle();
            mBundle.putBoolean(Constant.IsEditor , !mHeaderView.isDone());
            mBundle.putInt(Constant.FlightID , mSearchDataList.get(position).getFlightId());
            mIntent.putExtras(mBundle);
            turnActForResult(mIntent);
        }
    }

    private void initHeaderView() {
        mHeaderView.addOnHeaderListener(new HeaderTitleView.OnHeaderListener(){
            @Override
            public void onPartChange(int isChecked) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }

            @Override
            public void onDateChange(String date) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }

            @Override
            public void onClick(int resource) {
                FlightDialog.getInstance(mActivity)
                        .setConfirmButton(new FlightDialog.OnFlightOnClickListener() {
                            @Override
                            public void onClick(List<FlightData> flightList) {
                                mIntent = new Intent(mActivity , EnterPortWarehousingDetailsActivity.class);
                                mBundle = new Bundle();
                                mBundle.putInt(Constant.FlightID , flightList.get(0).getId());
                                mBundle.putBoolean(Constant.IsEditor , !mHeaderView.isDone());
                                mIntent.putExtras(mBundle);
                                turnActForResult(mIntent);
                            }
                        }).show();
            }

            @Override
            public void onSearch(String search) {
                initConnect();
            }
        });
    }

    public void onScanResult(String search){
        mSearchDataList = new ArrayList<>();
        if(!TextUtils.isEmpty(search)) {
            for (int i = 0; i < mWarehousingDataList.size(); i++)
                if(mWarehousingDataList.get(i).getFlightNo() != null && mWarehousingDataList.get(i).getFlightNo().contains(search))
                    mSearchDataList.add(mWarehousingDataList.get(i));
        }else {
            mSearchDataList.addAll(mWarehousingDataList);
        }

        initAdapter();
    }

    public void onClickSort(View view){
        mSortState = (mSortState != 0 && R.id.mTvFlightNumber == view.getId()) ? 0 : (mSortState == 0 ? -1 : mSortState) ;
        mSortState = (mSortState != 1 && R.id.mTvFlightDate == view.getId()) ? 1 : mSortState == 1 ? -1 : mSortState ;
        mSortState = (mSortState != 2 && R.id.mTvAta == view.getId()) ? 2 : mSortState == 2 ? -1 : mSortState ;
        mSortState = (mSortState != 3 && R.id.mTvRouteLine == view.getId()) ? 3 : mSortState == 3 ? -1 : mSortState ;
        mSortState = (mSortState != 4 && R.id.mTvCount == view.getId()) ? 4 : mSortState == 4 ? -1 : mSortState ;
        mSortState = (mSortState != 5 && R.id.mTvWeight == view.getId()) ? 5 : mSortState == 5 ? -1 : mSortState ;
        mSortState = (mSortState != 6 && R.id.mTvInventoryPerson == view.getId()) ? 6 : mSortState == 6 ? -1 : mSortState ;
        mSortState = (mSortState != 7 && R.id.mTvOverTime == view.getId()) ? 7 : mSortState == 7 ? -1 : mSortState ;

        mTvFlightNumber.setTextColor(mActivity.getColor(mSortState == 0 ? R.color.color_red : R.color.color_blue));
        mTvFlightDate.setTextColor(mActivity.getColor(mSortState == 1 ? R.color.color_red : R.color.color_blue));
        mTvAta.setTextColor(mActivity.getColor(mSortState == 2 ? R.color.color_red : R.color.color_blue));
        mTvRouteLine.setTextColor(mActivity.getColor(mSortState == 3 ? R.color.color_red : R.color.color_blue));
        mTvCount.setTextColor(mActivity.getColor(mSortState == 4 ? R.color.color_red : R.color.color_blue));
        mTvWeight.setTextColor(mActivity.getColor(mSortState == 5 ? R.color.color_red : R.color.color_blue));
        mTvInventoryPerson.setTextColor(mActivity.getColor(mSortState == 6 ? R.color.color_red : R.color.color_blue));
        mTvOverTime.setTextColor(mActivity.getColor(mSortState == 7 ? R.color.color_red : R.color.color_blue));

        initAdapter();
    }

    /**
     * 排序集合
     */
    private class SortComparator implements Comparator<EnterPortWarehousingData> {

        private int type;

        public SortComparator(int type){
            this.type = type;
        }

        @Override
        public int compare(EnterPortWarehousingData data1, EnterPortWarehousingData data2) {
            switch (type){
                case 0:
                    return ChinaToPinyin.getInstance().sort(data1.getFlightNo() , data2.getFlightNo());
                case 1 :
                    // 航班日期
                    return (int) (CommonUtils.getTimeLong(data2.getFldt()) - CommonUtils.getTimeLong(data1.getFldt()));
                case 2 :
                    // 进港实落
                    return (int) (CommonUtils.getTimeLong(data2.getAta()) - CommonUtils.getTimeLong(data1.getAta()));
                case 3 :
                    // 航线
                    return ChinaToPinyin.getInstance().sort(data1.getFlightCourseCode() , data2.getFlightCourseCode());
                case 4 :
                    return data2.getNum() - data1.getNum();
                case 5 :
                    return (int) (data2.getWeight() - data1.getWeight());
                case 6 :
                    return ChinaToPinyin.getInstance().sort(data1.getDispatchUserName() , data2.getDispatchUserName());
                case 7 :
                    // 录单完毕
                    return (int) (CommonUtils.getTimeLong(data2.getInOrderFinishDate()) - CommonUtils.getTimeLong(data1.getInOrderFinishDate()));
                default:
                    return 0 ;
            }
        }
    }
}
